local xianyi = fk.CreateSkill{
  name = "hanqing__xianyi",
}

Fk:loadTranslationTable{
  ["hanqing__xianyi"] = "贤仪",
  [":hanqing__xianyi"] = "一张牌指定多个目标后，若不为红色，你可以令一名目标角色选择是否将手牌弃至所有目标中最少并回复1点体力。",

  ["#hanqing__xianyi-choose"] = "贤仪：你可以令一名角色选择是否将手牌弃至%arg张并回复1点体力",
  ["#hanqing__xianyi-discard"] = "贤仪：你可以将手牌弃至%arg张，回复1点体力",
}

xianyi:addEffect(fk.TargetSpecified, {
  anim_type = "support",
  can_trigger = function (self, event, target, player, data)
    return player:hasSkill(xianyi.name) and
      data.firstTarget and #data.use.tos > 1 and data.card.color ~= Card.Red and
      table.find(data.use.tos, function (p)
        return not p.dead and table.find(data.use.tos, function (q)
          return p:getHandcardNum() > q:getHandcardNum() and not q.dead
        end) ~= nil
      end)
  end,
  on_cost = function (self, event, target, player, data)
    local room = player.room
    local n = 999
    for _, p in ipairs(data.use.tos) do
      if not p.dead then
        n = math.min(n, p:getHandcardNum())
      end
    end
    local targets = table.filter(data.use.tos, function (p)
      return not p.dead and p:getHandcardNum() > n
    end)
    local to = room:askToChoosePlayers(player, {
      min_num = 1,
      max_num = 1,
      targets = targets,
      skill_name = xianyi.name,
      prompt = "#hanqing__xianyi-choose:::"..n,
      cancelable = true,
    })
    if #to > 0 then
      event:setCostData(self, {tos = to, choice = n})
      return true
    end
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    local to = event:getCostData(self).tos[1]
    local n = event:getCostData(self).choice
    local num = to:getHandcardNum() - n
    if #room:askToDiscard(to, {
      min_num = num,
      max_num = num,
      include_equip = false,
      skill_name = xianyi.name,
      prompt = "#hanqing__xianyi-discard:::"..n,
      cancelable = true,
    }) == num and not to.dead then
      room:recover{
        who = to,
        num = 1,
        recoverBy = player,
        skillName = xianyi.name,
      }
    end
  end,
})

return xianyi
